//
//  RCOriginalView.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/12.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit
import SnapKit

let margin:CGFloat = 10

class RCOriginalView: UIView {

    var bottomConstraint:Constraint?
    
    //OC中 在模型这个属性的set方法里给其他属性赋值
    //swift中 在模型这个属性的didSet中给其他属性赋值
    var userStatus:RCUserStatus? {
        didSet{
            //绑定模型数据  模型一被赋值 就给属性们赋值
            iconView.sd_setImageWithURL(userStatus?.user?.headImageURL)
            nameLab.text = userStatus?.user?.name
            mbrankImage.image = userStatus?.user?.mbrank_image
            verifiedImage.image = userStatus?.user?.verified_type_image
            
            //TODO: 后期完善  OC中可以使用警告来标示待做事项
//            timeLab.text = userStatus?.created_at
//            sourceLab.text = userStatus?.source
            textLab.text = userStatus?.text
            //卸载约束
            bottomConstraint?.uninstall()
            //给pictureView的imageUrls赋值 有图片的微博就把图片URL传过去 没有就穿nil
            if let urls = userStatus?.imageUrl where urls.count != 0 {
                pictureView.imageUrls = urls
                pictureView.hidden = false
                self.snp_updateConstraints { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView).constraint
                    
                }
                
            }else {
                //没有图片的时候
                pictureView.imageUrls = nil
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(textLab).constraint
                })
                
            }
        }
        
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        
        addSubview(iconView)
        addSubview(nameLab)
        addSubview(mbrankImage)
        addSubview(verifiedImage)
        addSubview(timeLab)
        addSubview(sourceLab)
        addSubview(textLab)
        addSubview(pictureView)
        
        setContraints()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加约束
    func setContraints() {

        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.left.equalTo(self).offset(margin)
            make.width.height.equalTo(36)
        }
        iconView.layer.cornerRadius = 18
        iconView.clipsToBounds = true
        
        nameLab.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
            
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(nameLab.snp_right).offset(margin)
            
        }
        
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.right.bottom.equalTo(iconView)
            
        }
        
        timeLab.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
            
        }
        
        sourceLab.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(timeLab.snp_right).offset(margin)
        }
        
        textLab.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            //要想label换行 就要设置宽度和行数
            make.right.equalTo(self.snp_right).offset(-margin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLab.snp_bottom).offset(10)
            make.left.equalTo(textLab.snp_left)
            
        }
        
        //每个部分视图的底部约束，和自己子视图的地步不要有偏移量 offset 不然会多出来一块
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
            
        }
        
    }
    
    
    //头像
    lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
   
    //会员等级
    lazy var mbrankImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level6"))
    
    //认证
    lazy var verifiedImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //昵称
    lazy var nameLab:UILabel = UIFactory.labelWith("123", textColor: UIColor.purpleColor(), fontSize: 18, alignment: NSTextAlignment.Left)
    
    //发布时间
    lazy var timeLab:UILabel = UIFactory.labelWith("今天 23:59:59", textColor: UIColor.darkGrayColor(), fontSize: 12, alignment: NSTextAlignment.Left)
    
    //来源
    lazy var sourceLab:UILabel = UIFactory.labelWith("来自外太空的你", textColor: UIColor.darkGrayColor(), fontSize: 12, alignment: NSTextAlignment.Left)
    
    //原创正文
    lazy var textLab:UILabel = UIFactory.labelWith("qwe", textColor: UIColor.blackColor(), fontSize: 18, alignment: NSTextAlignment.Left)
    
    //图片视图
    lazy var pictureView:RCPictureView = RCPictureView()
    
}
